﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using Lbb.Data;
using Lbb.Localization;
using Lbb.URating.DL;
using Lbb.URating.Localization;

namespace HU8.UCenter.Plugins.URating.DAL
{
    /// <summary>
    /// 用户评分操作库
    /// </summary>
    /// <author>Chiron</author>
    public class U_RatingRepository : RepositoryBase<U_Rating>
    {
        #region Ctors
        /// <summary>
        /// 构造用户评分操作库
        /// </summary>
        /// <param name="db"></param>
        public U_RatingRepository(URatingDataContext db)
            : base(db)
        {
        }
        #endregion Ctors

        #region Get
        /// <summary>
        /// 取得用户评分
        /// </summary>
        /// <param name="id">用户评分ID</param>
        /// <returns></returns>
        public U_Rating Get(Guid? id)
        {
            return id.HasValue && id != Guid.Empty ? Get(d => d.UserRatingID == id) : null;
        }
        #endregion Get

        #region Find
        /// <summary>
        /// 搜索用户评分
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="categoryIds">评分分类</param>
        /// <param name="clickIds">点击器</param>
        /// <param name="objTypes">关联类型</param>
        /// <param name="objIds">关联对象</param>
        /// <param name="ratingIds">评分项</param>
        /// <param name="minRating">评分下限</param>
        /// <param name="maxRating">评分上限</param>
        /// <returns></returns>
        public IQueryable<U_Rating> Find(string keyword = null, IEnumerable<Guid> categoryIds = null, IEnumerable<Guid> clickIds = null,
            IEnumerable<string> objTypes = null, IEnumerable<string> objIds = null, IEnumerable<Guid> ratingIds = null,
             decimal? minRating = null, decimal? maxRating = null)
        {
            var data = GetAll();

            if (!string.IsNullOrEmpty(keyword)) data = data.Where(d => d.Username.Contains(keyword) || d.UserID.ToString().Contains(keyword) || d.U_ClickRating.U_Click.Title.Contains(keyword) || d.U_ClickRating.U_Click.U_ClickCategory.Title.Contains(keyword));

            if (ratingIds != null && (ratingIds = ratingIds.Where(d => d != Guid.Empty).ToList()).Count() > 0) data = data.Where(d => ratingIds.Contains(d.RatingID));

            if (clickIds != null && (clickIds = clickIds.Where(d => d != Guid.Empty).ToList()).Count() > 0) data = data.Where(d => clickIds.Contains(d.U_ClickRating.ClickID));

            if (objIds != null && (objIds = objIds.Where(d => string.IsNullOrEmpty(d)).ToList()).Count() > 0) data = data.Where(d => objIds.Contains(d.U_ClickRating.ObjectID));

            if (categoryIds != null && (categoryIds = categoryIds.Where(d => d != Guid.Empty).ToList()).Count() > 0) data = data.Where(d => categoryIds.Contains(d.U_ClickRating.U_Click.CategoryID));

            if (objTypes != null && (objTypes = objTypes.Where(d => string.IsNullOrEmpty(d)).ToList()).Count() > 0) data = data.Where(d => objTypes.Contains(d.U_ClickRating.U_Click.U_ClickCategory.ObjectType));

            if (minRating.HasValue) data = data.Where(d => d.Rating >= minRating.Value);

            if (maxRating.HasValue) data = data.Where(d => d.Rating <= maxRating.Value);

            return data;
        }
        #endregion Find

        #region Override
        /// <summary>
        /// 添加用户评分
        /// </summary>
        /// <param name="obj">用户评分</param>
        public override void Add(U_Rating obj)
        {
            if (obj != null && obj.UserRatingID == Guid.Empty) obj.UserRatingID = Guid.NewGuid();
            base.Add(obj);
        }
        /// <summary>
        /// 更新用户评分
        /// </summary>
        /// <param name="obj">用户评分</param>
        public override void Update(U_Rating obj)
        {
            CheckArg(obj);
            var old = Get(obj.UserRatingID);
            if (old == null) throw new ArgumentNullException(string.Format(OperatorRes.NotFindEditingObj, U_RatingRes.ClassName));

            try
            {
                Table.Attach(obj, old);
            }
            catch (InvalidOperationException)
            {
                DB.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
            }
        }
        #endregion Override

    }
}